<template>
  <div>
    <p>
      <button v-on:click="list(1)" class="btn btn-app btn-light btn-xs">
        <i class="ace-icon fa fa-refresh bigger-160"></i>
        刷新
      </button>
      &nbsp;&nbsp;

    </p>
    <div class="row">
      <div class="col-md-6">
        <textarea id="resource-textarea" class="form-control" v-model="resourceStr" name="resource" rows="10"></textarea>

        <br>
        <button id="save-btn" type="button" class="btn btn-primary" v-on:click="save()">
          保存
        </button>
      </div>
      <div class="col-md-6">
        <ul id="tree" class="ztree"></ul>
      </div>
    </div>
  </div>
</template>
<script>
  // import  Pagination from "../../components/pagination"
  export default {
    // components: {Pagination},
    name: 'system-resource',
    data: function () {
      return {
        resource: {},
        resources: [],
        resourceStr:"",
        tree:{},
      }
    },
    mounted: function () {
      let _this = this;
      // _this.$refs.pagination.size = 5;
      _this.list();
    },
    methods: {
      /**
       * 列表查询
       */
      list() {
        let _this = this;
        Loading.show();
        _this.$ajax.get(process.env.VUE_APP_SERVER+"/system/admin/resource/load-tree", {
        }).then((res)=>{
          Loading.hide();
          let response = res.data;
          _this.resources = response.content;
          //初始化树
          _this.initTree();
        })
      },
      /**
       * 点击保存
       * @param page
       */
      save() {
        let _this = this;
        //保存校验
        if(Tool.isEmpty(_this.resourceStr)){
           Toast.warning("资源不能为空！");
           return;
        }
        let json = JSON.parse(_this.resourceStr);

        Loading.show();
        _this.$ajax.post(process.env.VUE_APP_SERVER+"/system/admin/resource/save",
          json).then((response) => {
          Loading.hide();
          let resp = response.data;
          if (resp.success) {
            $("#form-modal").modal("hide");
            _this.list(1);
            Toast.success("保存成功！");
          }
          else {
            Toast.warning(resp.message);
          }

        })
      },
      /**
       * 点击删除
       * @param id
       */
      del(id) {
        let _this = this;
        Confirm.show("删除资源后不可恢复，确认？", function () {
          Loading.show();
          _this.$ajax.delete(process.env.VUE_APP_SERVER + "/system/admin/resource/delete/" + id).then((response) => {
            Loading.hide();
            let resp = response.data;
            if (resp.success) {
              _this.list(1);
              Toast.success("删除成功!")
            }
          })
        });
      },
        /**
         * 初始资源树
         */
        initTree() {
          let _this = this;
          let setting = {
            data: {
              simpleData: {
                idKey: "id",
                pIdKey: "parent",
                rootPId: "",
                // enable: true
              }
            }
          };

          _this.zTree = $.fn.zTree.init($("#tree"), setting, _this.resources);
          _this.zTree.expandAll(true);
          //全部展开
        },
      }
    }
</script>